package jmine.tec.hydra.web.pages.requisicao;

import java.util.List;

import jmine.tec.hydra.api.HydraAction;
import jmine.tec.hydra.domain.HydraComment;

import org.apache.wicket.markup.html.basic.Label;
import org.apache.wicket.markup.html.list.ListItem;
import org.apache.wicket.markup.html.list.ListView;
import org.apache.wicket.markup.html.panel.Panel;
import org.apache.wicket.model.PropertyModel;
import org.hibernate.Hibernate;

import bancosys.tec.persist.controller.PersistenceEnabledController;
import bancosys.tec.persist.hibernate.HibernateUtil;

/**
 * Modal panel que mostra detalhes de uma ação do hydra.
 * 
 * @author finx
 * @created Jan 21, 2011
 */
public class ModalPanelDetalheHydraAction extends Panel {

    private List<HydraComment> comments;

    /**
     * Construtor. Recebe o wicketId do modal panel, um controller e uma ação do hydra.
     * 
     * @param wicketId o wicketId do modal panel.
     * @param controller o controller.
     * @param hydraAction a ação do hydra.
     */
    public ModalPanelDetalheHydraAction(String wicketId, PersistenceEnabledController controller, HydraAction hydraAction) {
        super(wicketId);
        new HibernateUtil().reassociateObject(hydraAction);
        Hibernate.initialize(hydraAction.getComments());
        this.comments = hydraAction.getComments();
        this.generateComponents();
    }

    /**
     * Gera os componentes utilizados pelo modal panelz
     */
    private void generateComponents() {
        ListView<HydraComment> actionsListView =
                new ListView<HydraComment>("comments", new PropertyModel<List<HydraComment>>(this, "comments")) {

                    @Override
                    protected void populateItem(ListItem<HydraComment> item) {
                        HydraComment comment = item.getModelObject();
                        item.add(new Label("origin", comment.getOrigin().name()));
                        item.add(new Label("author", comment.getAuthor()));
                        item.add(new Label("time", comment.getTime().toString()));
                        item.add(new Label("comment", comment.getComment()));

                    }
                };
        actionsListView.setOutputMarkupId(true);
        this.add(actionsListView);
    }

    /**
     * @return the comments
     */
    public List<HydraComment> getComments() {
        return this.comments;
    }

    /**
     * @param comments the comments to set
     */
    public void setComments(List<HydraComment> comments) {
        this.comments = comments;
    }

}
